/*
 * CS_REQ_AI_BATTLE_RESULT.cpp
 *
 *  Created on: 2013. 10. 28.
 *      Author: jjaehuny
 */

#include "../../Command.h"

#include "../../User.h"
#include "../../Proxy.h"

#include "../../NLogic/Bag.h"
#include "../../NLogic/Equip.h"


namespace ProjectZ { namespace NCommand {

void SendPacket(User * pUser, int res, int slot_number)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	pAck->Write2(res);
	pAck->Write1(slot_number);
	pAck->MakePacket(CMD_SC_REQ_CHANGE_MAIN_CHARACTER);

	pUser->Send(pAck);
}

void Command::CS_REQ_CHANGE_MAIN_CHARACTER(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	int slot_number = 0;
	pPacket->ParseMsg("%c", &slot_number);

	if (slot_number < 0 || slot_number > 7)
	{
		DEBUGLOG("CS_REQ_CHANGE_MAIN_CHARACTER slot_number < 0 || slot_number > 7 slot_number : %d", slot_number);
		SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
		return;
	}

	if (0 == pUser->GetUserInfo().array_Slot[slot_number].character_seq)
	{
		DEBUGLOG("CS_REQ_CHANGE_MAIN_CHARACTER 0 == pUser->GetUserInfo().array_Slot[slot_number].character_seq slot_number : %d", slot_number);
		SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
		return;
	}

//	if (false == pUser->GetUserInfo().array_Slot[slot_number].makeCharacter)
//	{
//		DEBUGLOG("CS_REQ_CHANGE_MAIN_CHARACTER slot_number < 0 || slot_number > 7 slot_number : %d", slot_number);
//		SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
//		return;
//	}

	pUser->GetUserInfo().main_slot_index = slot_number;

	// 새로운 캐릭터의 데이터를 읽어 오기 위해 Bag정보 초기화
	for(int i = static_cast<int>(BAG_TYPE_NORMAL); i < static_cast<int>(BAG_TYPE_MAX); ++i)
	{
		NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(i));
		if (NULL == pBag)
		{
			SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
			return;
		}
		pBag->Finalize();
	}

	NLogic::Equip * pEquip = pUser->GetEquip();
	if (NULL == pEquip)
	{
		SendPacket(pUser, ACK_UNKNOWN_ERROR, slot_number);
		return;
	}
	pEquip->Finalize();

	//pUser->GetCharacterInfo().level = pUser->GetCharacterInfoFromIndex(slot_number).level;
	//pUser->GetCharacterInfo().exp = pUser->GetCharacterInfoFromIndex(slot_number).exp;

	pUser->ChangeMainSlot();

	SendPacket(pUser, ACK_OK, slot_number);

	NProxy::SetMainCharacterInfo(pUser);
}

} /*NCommand*/ } /*ProjectZ*/

